/*
  this link list program can be used to form table for compression
  @ vishwajeet dusane
  What this program does
	 -accept file name from user
	 -build table i.e. occurence of each char in the file
	 -sort them according to the number of char in file in descending order
*/


#include"BuildTable.h"
#include<math.h>

//int getSizeOfBits(int );
void  binary_2_number(int ,int );
int ans;
void main()
{
	struct node *head=NULL,*temp;

	BuildTable BT;
	int result,position,size,totalChar;
	int ch;
	char fname[50];
	FILE *fp,*fp1;
	printf("Enter File Name:");
	scanf("%s",fname);

	  fp=fopen(fname,"rb");
	 if(fp==NULL)
	 {
	 printf("Error While Openonig  %s",fname);
	 getch();
	 exit(0);
	 }

	while((ch=fgetc(fp))!=EOF)
	{
	 result=BT.search(&head,ch);/*whether the char is in the table or not
							   if it is in then no need to add that in
							   the table search will automatically
							   increment its count
							   */

	 if(result!=0)
	 BT.create(&head,ch);
	}
   //	BT.display(head);

	 BT.SortTable(&head);   /*This function can sort the nodes according to
						   the occurrences of a charcter
						 */
	// clrscr();
	//  BT.display(head);
						 /*this function will display the sorted nodes
							can be used for display (those which r not sorted)
							sort it in descending order
						*/
	  BT.writeTable(head);
	totalChar=BT.getTotalChar(head);
  fclose(fp);
 fp=fopen(fname,"rb");
 fp1=fopen("c:/temp.neo","wb");
 if(fp==NULL||fp1==NULL)
 {
  printf("Could not open file") ;
  getch();
  exit(0);
 }
   size=getSizeOfBits(totalChar);
   if(size==0)
   {
	 printf("Can't use this compression");
	 getch();

   }
	while((ch=fgetc(fp))!=EOF)
	{
	  position=BT.getPosition(head,ch);
	   if(position==0)
	   {
	   printf("Error During Compression");
	   getch();
	   return ;
	   }


	   int i,k,mask;
 // printf("%c  %d\n",ch,ch);
   for(i=size-1;i>=0;i--)
	{
	 mask=1<<i;
	 k=position&mask;
	 if(k==0)
	 fputc('0',fp1);
	  else
	 fputc('1',fp1);
	}

 }
fclose(fp1);
fclose(fp);
	fp=fopen("c:/huff.txt","wb");
	if(fp==NULL)
	{
	 printf("Error While Opening huff.txt");
	 getch();
	 exit(0);
	}
	temp=head;
	while(temp!=NULL)
	{
	 fputc(temp->ch,fp);
	 temp=temp->next;
	}
	fputc('`',fp);
   fp1=fopen("c:/temp.neo","rb");
	if(fp1==NULL)
	{
	 printf("Error While Opening temp.neo");
	 getch();
	 exit(0);
	}

   ch=0;
	 while(ch!=EOF)
	 {
	 ans=0;
	  for(int i=7;i>=0;i--)
	   {
		ch=fgetc(fp1);
			if(ch==EOF)
		 break;
 //		printf("i=%d  ch=%d\n",i,ch);
		binary_2_number(ch,i);
  //		printf("  ans=%d",ans);
		   }
	  // printf("%d\n",ans);
	   fputc(ans,fp);
	//   printf("%c  %d\n",ans,ans);
	//   getch();

  }
//  fputc(EOF,fp);
fcloseall();
 if(unlink("c:/TEMP.NEO")==0)
 printf("OK");
}


  void  binary_2_number(int ch,int i)
{
   int temp;
   if(ch=='1')
   {
	 temp=pow(2,i);
	// printf("  temp=%d",temp);
	 ans=ans+temp;
   }
   else
	 ans=ans+0;
}

/*
int getSizeOfBits(int totaChar)
{

	if(totaChar==1 || totaChar==0)
	{
	   return 1;

	}
	else

	if(totaChar>1||totaChar>=3)
	{
	   return 2;

	}

	else

	if(totaChar<=7)
	{
	   return 3;

	}
	else

	if(totaChar<=15)
    {
       return 4;  
       
    }             
    else

	if(totaChar<=31)
    {
       return 5;  
       
    }             
	else

    if(totaChar<=63)
	{
       return 6;  
       
    }             
    else

    if(totaChar<=127)
    {
       return 7;  
       
    }             
	else

	if(totaChar>=128)
	{
	   return 0;

	}
}
  */

